package com.mchsdk.paysdk.http.request;

import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.entity.UserRegister;
import com.mchsdk.paysdk.http.RequestUtil;
import com.mchsdk.paysdk.utils.MCErrorCodeUtils;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.VerifyCodeCookisStore;

import org.json.JSONException;
import org.json.JSONObject;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

public class RegisterRequest {

    private static final String TAG = "RegisterRequest";

    HttpUtils http;
    Handler mHandler;
    String mUserName;
    String mPassword;

    public RegisterRequest(Handler mHandler, String userName, String password) {
        http = new HttpUtils();
        if (null != mHandler) {
            this.mHandler = mHandler;
        }
        if (!TextUtils.isEmpty(userName)) {
            this.mUserName = userName;
        } else {
            this.mUserName = "";
        }
        if (!TextUtils.isEmpty(password)) {
            this.mPassword = password;
        } else {
            this.mPassword = "";
        }
    }

    public void post(String url, RequestParams params) {
        if (TextUtils.isEmpty(url) || null == params) {
            MCLog.e(TAG, "fun#post url is null add params is null");
            noticeResult(Constant.REGISTER_FAIL, "url未设或参数为空");
            return;
        }
        MCLog.e(TAG, "fun#post url = " + url);
        http.configCookieStore(VerifyCodeCookisStore.cookieStore);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                MCLog.e(TAG, "fun#responseInfo.result = " + responseInfo.result);
                String response = RequestUtil.getResponse(responseInfo);

                UserRegister registerSuccess = new UserRegister();
                registerSuccess.setUserName(mUserName);
                registerSuccess.setPassword(mPassword);
                String res = "注册失败";
                int status;
                JSONObject json = null;
                try {
                    json = new JSONObject(response);
                    status = json.optInt("status");
                    res = json.optString("return_msg");
                    registerSuccess.setStatus("1");
//					MCLog.e(TAG, "onSuccess" + res);
                } catch (JSONException e) {
                    res = "数据解析异常";
                    status = 0;
                } catch (Exception e) {
                    res = "数据解析异常";
                    status = 0;
                }

                if (status == 200 || status == 1) {
                    noticeResult(Constant.REGISTER_SUCCESS, registerSuccess);
                } else {
                    String msg;
                    if (!TextUtils.isEmpty(json.optString("return_msg"))) {
                        msg = json.optString("return_msg");
                    } else {
                        msg = MCErrorCodeUtils.getErrorMsg(status);
                    }
                    MCLog.e(TAG, "msg:" + msg);
                    registerSuccess.setRegisterResult(msg);
                    noticeResult(Constant.REGISTER_FAIL, msg);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                MCLog.e(TAG, "onFailure" + msg);
                MCLog.e(TAG, "onFailure" + error.getExceptionCode());
                MCLog.e(TAG, "onFailure" + error.getLocalizedMessage());
                MCLog.e(TAG, "onFailure" + error.getMessage());
                MCLog.e(TAG, "onFailure" + error.getStackTrace());
                noticeResult(Constant.REGISTER_FAIL, "网络异常");
            }
        });
    }

    private void noticeResult(int type, Object obj) {
        Message msg = new Message();
        msg.what = type;
        msg.obj = obj;
        if (null != mHandler) {
            mHandler.sendMessage(msg);
        }
    }

}
